<?php

namespace app\admin\controller;

use controller\BasicAdmin;
use app\admin\service\NodeService;
use library\tools\Data;
use think\Console;
use think\Db;
use think\exception\HttpResponseException;

/**
 * 系统公共操作
 * Class Index
 * @package app\admin\controller
 */
class Qrcodes extends BasicAdmin
{

    public $table = 'qrcodes';
    /**
     * 模板页列表
     */
    public function index()
    {
        $this->title = '二维码管理';
        $get = $this->request->get();
        $db = Db::name($this->table)->where(['is_delete' => '0']);
        foreach (['name'] as $key) {
            (isset($get[$key]) && $get[$key] !== '') && $db->whereLike($key, "%{$get[$key]}%");
        }
        if (isset($get['date']) && $get['date'] !== '') {
            list($start, $end) = explode('-', str_replace(' ', '', $get['date']));
            $db->whereBetween('create_time', ["{$start} 00:00:00", "{$end} 23:59:59"]);
        }
        foreach ($db as $key=>$value)
        {
            $type = Db::table('qrcodes_type')->find($value['type']);
            $db[$key]['typename'] = $type['name'];
        }
        $map['qr.is_delete'] = array('eq',0);
        if(isset($_GET['name'])){
            if($_GET['name']){
                $map['qr.name'] = array("like","%".$_GET['name']."%");
            }
            if($_GET['type']){
                $map['qr.type'] = array('eq',$_GET['type']);
            }
        }

        $qrlist = Db::table($this->table)
            ->alias('qr')
            ->field('qr.*,qt.name as typename')
            ->where($map)
            ->join('qrcodes_type qt','qt.id = qr.type','left')
            ->paginate(10);
        p($qrlist);
        $this->assign('qrlist',$qrlist);

        $types = Db::table('qrcodes_type')->where(['is_delete'=>0])->select();
        $this->assign('menus',$types);

        return parent::_list($db);
    }

    public function _page_filter_index($data)
    {
        $this->qrlist = Db::table($this->table)
            ->alias('qr')
            ->field('qr.*,qt.name as typename')
            ->where('qr.is_deleted',0)
            ->join('qrcodes_type qt','qt.id = qr.type','left')
            ->paginate(10);
    }

    // 添加
    public function add()
    {
        $types = Db::table('qrcodes_type')->where(['is_delete'=>0])->select();
        $this->assign('menus',$types);
       // var_dump('aaa'); die();
        $this->title = '添加二维码';

        if($_POST){
            $data = $_POST;
            //$data['create_time'] = time();
            $flag = Db::name('qrcodes')->insert($data);
            if($flag){
                $this->redirect('index');
            }else{
                return $this->fetch('form');
            }
        }

        return $this->fetch('form');
       // $this->_form($this->table, 'form');
    }

    // 编辑
    public function edit()
    {
        $types = Db::table('qrcodes_type')->where(['is_delete'=>0])->select();
        $this->assign('menus',$types);
        $data = $_GET;
        if($data['id']){
            $qrcode = Db::table('Qrcodes')->find($data['id']);
            $this->assign('qrcode',$qrcode);
        }
        $this->_form($this->table, 'form');
    }

    // 禁用
    public function forbid()
    {
        $this->_save($this->table, ['status' => '0']);
    }

    // 启用
    public function resume()
    {
        $this->_save($this->table, ['status' => '1']);
    }

    // 删除
    // 删除
    public function del()
    {
        $data = $_POST['id'];
        $flag = Db::table('qrcodes_type')->where('id in ('.$data.')')->update(['is_delete'=>1]);

        $this->redirect('index');
    }





}
